package wxshop.service.user.impl;

import wxshop.api.user.dao.UserDao;
import wxshop.api.user.exception.DataNotFoundException;
import wxshop.api.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wxshop.api.user.service.LoginService;

/**
 * Created by yvdedu.com on 2018/1/24.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
public class LoginServiceimpl implements LoginService {
	//登录方法
	@Autowired
	private UserDao userDao;
	
	@Override
	public User login (User input) {
		User user = userDao.getByLogin(input.getLogin());
		if (user==null) {
			throw new DataNotFoundException("账号你不存在");
		} else if (!user.getPassword().equals(input.getPassword())) {
			throw new DataNotFoundException("密码错误");
		} else {
			return user;
		}
	}
	
	public int zhuce (User input) {
		User user = userDao.getByLogin(input.getLogin());
		if (user!=null) {
			System.out.println(user);
			throw new DataNotFoundException("账号重复,请重新输入");
		}
		int id = userDao.insert(input);
		System.out.println(id);
		if (id < 0 ) {
			throw new DataNotFoundException("插入错误");
		}  else {
			return id;
		}
	}
}
